package com.xuecheng.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.ucenter.mapper.XcUserMapper;
import com.xuecheng.ucenter.model.dto.AuthParamsDto;
import com.xuecheng.ucenter.model.dto.FindPasswordDto;
import com.xuecheng.ucenter.model.po.XcUser;
import com.xuecheng.ucenter.service.FindPasswordService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class FindPasswordServiceImpl implements FindPasswordService {

    @Autowired
    private XcUserMapper xcUserMapper;

    @Override
    @Transactional
    public String findPassword(FindPasswordDto findPasswordDto) {
        //先进行验证码校验
//        String checkcodekey = authParamsDto.getCheckcodekey();
        String checkcodekey = "1234";
        String checkcode = findPasswordDto.getCheckcode();
        if (!check(checkcode, checkcodekey)) {
            throw new RuntimeException("验证码错误");
        }

        //首先确定是进行什么验证
        Long cellphone = findPasswordDto.getCellphone();
        if (cellphone != null) {
            //进行的是手机号验证，去数据库查找手机号
            XcUser xcUser = xcUserMapper.selectOne(new LambdaQueryWrapper<XcUser>().eq(XcUser::getCellphone, cellphone));
            if (xcUser == null) {
                throw new RuntimeException("请确认该手机号是否进行注册");
            }
            XcUser xcUser_update = updatePassword(xcUser, findPasswordDto);
            int update = xcUserMapper.update(xcUser_update, new LambdaQueryWrapper<XcUser>().eq(XcUser::getCellphone, cellphone));
            if (update > 0) {
                return "修改成功";
            }
            throw new RuntimeException("发生错误");
        }


        String email = findPasswordDto.getEmail();
        if (StringUtils.isEmpty(email)) {
            return "请设置参数";
        }
        XcUser xcUser = xcUserMapper.selectOne(new LambdaQueryWrapper<XcUser>().eq(XcUser::getEmail, email));
        if (xcUser == null) {
            throw new RuntimeException("请确认该邮箱是否进行注册");
        }
        XcUser xcUser_update = updatePassword(xcUser, findPasswordDto);
        int update = xcUserMapper.update(xcUser_update, new LambdaQueryWrapper<XcUser>().eq(XcUser::getEmail, email));
        if (update > 0) {
            return "修改成功";
        }

        throw new RuntimeException("发生错误");
    }

    /**
     * 校验参数
     *
     * @return
     */
    private boolean check(String param1, String param2) {
        if (param1 == null) {
            return false;
        }
        if (param1.equals(param2)) {
            return true;
        }
        return false;
    }

    private XcUser updatePassword(XcUser xcUser, FindPasswordDto findPasswordDto) {
        String confirmpwd = findPasswordDto.getConfirmpwd();
        String password = findPasswordDto.getPassword();
        //进行了注册，更换密码
        if (!check(confirmpwd, password)) {
            throw new RuntimeException("两次密码不一致");
        }
        String password_db = xcUser.getPassword();
        if (check(password, password_db)) {
            throw new RuntimeException("不能修改一样的密码");
        }
        xcUser.setPassword(new BCryptPasswordEncoder().encode(password));
        return xcUser;
    }
}
